$(function(){
	var host = window.location.host;
	var domainName = "http://" + host;
	var url = window.location.pathname;
	var aMenuOneLi = $(".nav nav a");
	if(aMenuOneLi)
	{
		if (url != "/" && url != "" && url.toLowerCase() != "/PhpFlash/index") {
		  if ($(".nav nav a[href='" + url + "']").length > 0) {
			$(".nav nav a[href='" + url + "']").addClass("red").siblings().removeClass("red");
		  } else {
			$(".nav nav a[href='/user/index']").addClass("red").siblings().removeClass("red");
		  }
		} else {
		  $(aMenuOneLi[0]).addClass("red").siblings().removeClass("red");
		}
	}

	if ($("div[app-path='index']").length == 0) {
		// loadpagecom();
	}
});

/**
 * Created by Administrator on 2016/8/8.
 */
 //页面加载效果
function loadpage() {
    var load_html = '<div class="page_load" id="page_losd" style="position:fixed; width:100%; height:100%; background:#ffffff; z-index:999999; top:0; left:0; bottom:0; right:0;">' +
            '<div class="loader show">' +
    '<div class="act">' +
    '  <div class="spinner">  <div class="double-bounce1"></div>  <div class="double-bounce2"></div></div>' +
    ' </div>' +
    '<div class="bg"></div>' +
    '  <span class="text">页面加载中...</span>' +
    '</div></div>'
    document.write(load_html);
}
loadpage();
function close_loadpage() {
    var loadingMask = document.getElementById('page_losd');
    if(loadingMask)
    {
        loadingMask.parentNode.removeChild(loadingMask);
    }
}
document.onreadystatechange = loadpagecom;
//加载状态为complete时移除loading效果
function loadpagecom() {
    if (document.readyState == "complete") {
        var loadingMask = document.getElementById('page_losd');
        if(loadingMask)
        {
            loadingMask.parentNode.removeChild(loadingMask);
        }

    }
}
function postMsg(url, data, fun) {
    // data = JSON.stringify(data);
    return $.ajax({
        type: "POST",
        //contentType: "application/json",
        url: url,
        data: data,
        // timeout: 15000, //超时时间：15秒
        dataType: 'json',
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            netError();
            return;
        },
        complete: function (XMLHttpRequest, textStatus) {
            if (textStatus == "timeout") {
                netError();
                return;
            }
        },
        success: function (res) {
            if (!res || res == "") {
                netError();
                return;
            }
            if (res.url) {
                location.href = res.url;
                return;
            }
			
            fun(res);
        }
    });
}
function netError() {
	return true;
    loadhide();
    var error_msg = alertMsg("网络开了小差，操作会受影响哦,<br/>请您再尝试一次,将网络找回来.");
}
function getLocalTime(cellval) {
    // var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
    var date = new Date(cellval);
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var Minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var Seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + Minutes + ":" + Seconds;
}

function getLocal(cellval) {
    // var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
    var date = new Date(cellval);
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return date.getFullYear() + "-" + month + "-" + currentDate;
}

function loadshow() {
    $('.loader').addClass('show')
}

function loadhide() {
    $('.loader').removeClass('show')
}

function endFun() {
    console.log('关闭弹窗');
}

function alertMsg(obj) {
    if (obj.content) {
        if (!obj.className) obj.className = 'pop';
        return layer.open(obj);
    } else {
        return layer.open({
            content: obj,
            title: "提示",
            className: 'pop',
            btn: ['确定'],
            end: endFun,
        });
    }
}
function alertMsg2(obj) {

    if (obj.content) {
        if (!obj.className) obj.className = 'pop no_close';
        obj.shadeClose = false;
        return layer.open(obj);
    } else {
        return layer.open({
            content: obj,
            shadeClose: false,
            title: "提示",
            className: 'pop no_close',
            btn: ['确定'],
            end: endFun,
        });
    }
}

function alertMsg3(content)
{
	return layer.open({
	  title: '长按二维码支付',
	  closeBtn: 0,
	  shadeClose: false,
	  className: 'pop',
	  content:content
	});
}


function alertWarnQQ(msg, callback1, callback2, close) {
    if (typeof close == "undefined") {
        close = true;
    }
    layer.open({
        className: 'popuo-login1 pop no_close',
        shadeClose: false,
        title: "提示",
        content: '<div class="mo">' + msg + '</div>', //如果警告，就调用这个dom 
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            if (close)
                layer.close(index);
            if (callback1) {
                callback1(index, layero);
            }
        },
        no: function (index, layero) {
            if (callback2) {
                callback2(index, layero)
            }
        }

    });
}


function goURL(url) {
    window.location.href = url;
}
var m = {};
m.alertInfo = function (msg, callback) {
    layer.open({
        content: msg,
        className: "pop",
        title: "提示",
        btn: ['确定'],
        yes: function (index, layero) {
            layer.close(index);
            if (callback) {
                callback(index, layero);
            }
        }
    });
}
m.alertOK = function (msg, callback) {
    layer.open({
        className: 'pop',
        title: "成功提示",
        content: msg, //如果支付成功，就调用这个dom
        btn: ['确定'],
        yes: function (index, layero) {
            layer.close(index);
            if (callback) {
                callback(index, layero);
            }
        }
    });
}
m.alertError = function (msg, callback) {
    layer.open({
        content: msg,
        className: 'pop',
        title: "警告",
        btn: ['确定'],
        yes: function (index, layero) {
            layer.close(index);
            if (callback) {
                callback(index, layero);
            }
        }
    });
}


var loadIndex = 0;
m.loading = function (msg) {
    //加载层
    loadIndex = layer.open({
        type: 2,
        shadeClose: true,
        content: msg
    });
}
m.loadEnd = function () {
    layer.close(loadIndex);
}




/**
     * 异步加载依赖的javascript文件
     * src：script的路径
     * callback：当外部的javascript文件被load的时候，执行的回调
     */
function loadAsyncScript(src, callback) {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", src);
    script.setAttribute("async", true);
    script.setAttribute("defer", true);
    head.appendChild(script);

    //fuck ie! duck type
    if (document.all) {
        script.onreadystatechange = function () {
            var state = this.readyState;
            if (state === 'loaded' || state === 'complete') {
                callback();
            }
        }
    } else {
        //firefox, chrome
        script.onload = function () {
            callback();
        }
    }
}

function getAsteriskEnd(str, showLength) {
    if (!showLength) {
        showLength = 2;
    }
    if (str.length > showLength) {
        var asterisk = str.length - showLength;
        str = str.substring(0, showLength);
        for (var i = 0; i < asterisk; i++) {
            str += "*";
        }
    }
    return str;
}

function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


$(function () {
    if (window.top.location !== window.location) {
        top.location.href = self.location.href;
    }
});
var m = {};
m.alertInfo = function (msg, callback) {
    layer.open({
        content: msg,
        className: "pop",
        title: "提示",
        btn: ['确定'],
        yes: function (index, layero) {
            layer.close(index);
            if (callback) {
                callback(index, layero);
            }
        }
    });
}
m.alertOK = function (msg, callback) {
    layer.open({
        className: 'pop',
        title: "成功提示",
        content: msg, //如果支付成功，就调用这个dom
        btn: ['确定'],
        yes: function (index, layero) {
            layer.close(index);
            if (callback) {
                callback(index, layero);
            }
        }
    });
}
m.alertError = function (msg, callback) {
    layer.open({
        content: msg,
        className: 'pop',
        title: "警告",
        btn: ['确定'],
        yes: function (index, layero) {
            layer.close(index);
            if (callback) {
                callback(index, layero);
            }
        }
    });
}


var loadIndex = 0;
m.loading = function (msg) {
    //加载层
    loadIndex = layer.open({
        type: 2,
        shadeClose: true,
        content: msg
    });
}
m.loadEnd = function () {
    layer.close(loadIndex);
}

function loadImage(id, src, callback) {
    var imgloader = new window.Image();
    //当图片成功加载到浏览器缓存
    imgloader.onload = function (evt) {
        loadshow();
        if (typeof (imgloader.readyState) == 'undefined') {
            imgloader.readyState = 'undefined';
        }
        //在IE8以及以下版本中需要判断readyState而不是complete
        if ((imgloader.readyState == 'complete' || imgloader.readyState == "loaded") || imgloader.complete) {
            callback({ 'msg': 'ok', 'src': src, 'id': id });
            loadhide();
        } else {
            imgloader.onreadystatechange(evt);
        }
    };
    //当加载出错或者图片不存在
    imgloader.onerror = function (evt) {
        callback({ 'msg': 'error', 'id': id });
    }
    //当加载状态改变
    imgloader.onreadystatechange = function (e) {
        //此方法只有IE8以及一下版本会调用
    }

    imgloader.src = src;

}
